﻿using System;
using System.Collections;
using System.Collections.Generic;
using UniRx;
using UnityEngine;

public class AIStateStandSkill : AIStateBase
{
    protected override string GetName() => "standSkill";

    public override bool isLoop { get; } = false;

    private List<SkillBase> m_StandingSkills;
    private List<SkillBase> m_SkillsCanUse = new List<SkillBase>();

    protected override void OnInit()
    {
        m_StandingSkills = ai.entity.skillManager.skills.FindAll(s => s.config.effectiveStates.HasFlag(SkillEffectiveState.Standing));
    }

    protected override void OnExecute()
    {
        if (m_SkillsCanUse.Count <= 0)
        {
            isEnd = true;
            return;
        }

        var skillId = m_SkillsCanUse.RandomElement().config.id;
        ai.outputs.SetAxis("standSkill", skillId);

        if (!HaveSkillsCanUse())
        {
            isEnd = true;
        }
    }

    protected override void OnExit()
    {
        ai.outputs.SetAxis("standSkill", 0);
        var monster = ai.entity as MonsterEntity;
        if (monster.hatred != null && monster.hatred.attribute.currentHP <= 0)
            monster.hatred = null;
    }

    public bool HaveSkillsCanUse()
    {
        m_SkillsCanUse.Clear();
        for (int i = 0; i < m_StandingSkills.Count; i++)
        {
            var skill = m_StandingSkills[i];
            // TODO 添加冷却时间判断
            //if (skill.data.cooldown > 0) continue;
            if (skill.config.targetType == SkillTargetType.Enemy)
                if (skill.area.SearchArea().Count <= 0)
                    continue;
            m_SkillsCanUse.Add(m_StandingSkills[i]);
        }
        return m_SkillsCanUse.Count > 0;
    }
}